package com.yifeng.repo.controller.rocketmq.configure;

import com.yifeng.repo.controller.rocketmq.message.ProduceManager;
import com.yifeng.repo.controller.rocketmq.message.PullConsumeManager;
import com.yifeng.repo.controller.rocketmq.message.PushConsumeManager;
import com.yifeng.repo.controller.rocketmq.web.RocketmqServletRegistrar;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * Created by daibing on 2023/8/5.
 */
@Configuration
@EnableConfigurationProperties(RocketmqProperties.class)
@ConditionalOnClass({RocketmqProperties.class})
public class RocketmqConfiguration {

    @Value("${spring.application.name}")
    private String applicationName;

    @Resource
    private RocketmqProperties properties;

    @Bean(initMethod = "init", destroyMethod = "destroy")
    @ConditionalOnProperty(prefix = "yfcloud.controller.rocketmq", name = "enable-produce-manager")
    public ProduceManager produceManager() {
        return new ProduceManager(applicationName, properties);
    }

    @Bean(initMethod = "init", destroyMethod = "destroy")
    @ConditionalOnProperty(prefix = "yfcloud.controller.rocketmq", name = "enable-pull-consume-manager")
    public PullConsumeManager pullConsumeManager() {
        return new PullConsumeManager(applicationName, properties);
    }

    @Bean(initMethod = "init", destroyMethod = "destroy")
    @ConditionalOnProperty(prefix = "yfcloud.controller.rocketmq", name = "enable-push-consume-manager")
    public PushConsumeManager pushConsumeManager() {
        return new PushConsumeManager(applicationName, properties);
    }

    @Bean
    @ConditionalOnProperty(prefix = "yfcloud.controller.rocketmq", name = "enable-rocketmq-api")
    public RocketmqServletRegistrar rocketmqServletRegistrar() {
        return new RocketmqServletRegistrar(properties.getMqAdminPrefix());
    }
}
